﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class SavingMoneyFormBill : Form
    {
        MainForm mainform;
        public SavingMoneyFormBill(MainForm main)
        {
            InitializeComponent();
            mainform = main;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"]; 
        }

        decimal pageSize = 18;
        int pageNum = 1;
        int pages;
        decimal ss;
        int sousuo = 0;
        string strWhere = "";

        private void SavingMoneyFormBill_Load(object sender, EventArgs e)
        {
            this.Opacity = 0;
            this.timer1.Start();
            this.CbxAdmin.SelectedIndex = 0;
            DGV();
            CurrYe();
        }
        public void CurrYe()
        {
            wasteBook wast = new wasteBook();
            ZG_member.Model.wasteBook wastModel = new ZG_member.Model.wasteBook();
            decimal count = wast.GetList("savingMoney!=0").Tables[0].Rows.Count;
            string s = (count / pageSize).ToString();
            ss = Math.Floor(decimal.Parse(s));
            if (decimal.Parse(s) > ss)
            {
                ss++;
            }
            label2.Text = pageNum + "/" + ss;
        }
        public void DGV()
        {
            wasteBook wastebook = new wasteBook();
            DataTable dt = wastebook.Pagination(Convert.ToInt32(pageSize),pageNum,"savingMoney != 0").Tables[0];
            DgvConsumptionBill.DataSource = dt;
            DgvConsumptionBill.AllowUserToAddRows = false;

            operatorInfo opInfo = new operatorInfo();
            DataTable dataTable = opInfo.GetList("").Tables[0];
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                if (!CbxAdmin.Items.Contains(dataTable.Rows[i]["username"].ToString()))
                {
                    CbxAdmin.Items.Add(dataTable.Rows[i]["username"].ToString());
                }
            }
            if (DgvConsumptionBill.Rows.Count > 0)
            {
                for (int i = 0; i < DgvConsumptionBill.Rows.Count; i++)
                {
                    if (DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString() != "")
                    {
                        consumeSolution consume = new consumeSolution();
                        Model.consumeSolution consumeMod = new Model.consumeSolution();
                        consumeMod = consume.GetModel(int.Parse(DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString()));
                        DgvConsumptionBill.Rows[i].Cells["CardType"].Value = consumeMod.solutionName;
                    }
                }
            }
            LblTotal1.Text = DgvConsumptionBill.Rows.Count.ToString();
        }
        private void btnInquiry_Click(object sender, EventArgs e)
        {
            sousuo = 1;
            StringBuilder stringBuilder = new StringBuilder();
            if (CbxAdmin.Text != "所有操作员")
            {
                stringBuilder.Append("operatorId='" + CbxAdmin.Text.Trim() + "'");
                stringBuilder.Append("and ");
            }
            if (!string.IsNullOrEmpty(TxtConsumer.Text))
            {
                stringBuilder.Append("memberCardId='" + TxtConsumer.Text.Trim() + "'");
                stringBuilder.Append("and ");
            }
            stringBuilder.Append("savingMoney != 0 and ");
            if (DateTime.Parse(DtpDate.Text) <= DateTime.Parse(DtpDate1.Text))
            {
                stringBuilder.Append("date between '" + DateTime.Parse(DtpDate.Text) + "'and'" + DateTime.Parse(DtpDate1.Text) + "'");
                stringBuilder.Append("and ");
            }
            else
            {
                stringBuilder.Append("date=''");
                stringBuilder.Append("and ");
                MessageBox.Show("日期错误");
            }
            string strBuilder = stringBuilder.ToString();
            if (strBuilder.Substring(strBuilder.Length - 4, 4) == "and ")
            {
                strBuilder = strBuilder.Substring(0, strBuilder.Length - 4);
            }
            strWhere = strBuilder;

            wasteBook wastebook = new wasteBook();
            int count = wastebook.GetList(strBuilder).Tables[0].Rows.Count;
            decimal xiaoshu = count / pageSize;
            int zhengshu = Convert.ToInt32(Math.Floor(xiaoshu));
            if (xiaoshu > zhengshu)
            {
                zhengshu++;
            }
            ss = zhengshu;
            pageNum = 1;
            DataTable dt2= wastebook.Pagination(Convert.ToInt32(pageSize), pageNum, strWhere).Tables[0];
            this.label2.Text = pageNum + "/" + ss;
            DgvConsumptionBill.DataSource = dt2;
            DgvConsumptionBill.AllowUserToAddRows = false;
            consumeSolution consume = new consumeSolution();
            Model.consumeSolution consumeMod = new Model.consumeSolution();
            if (dt2.Rows.Count > 0)
            {
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    if (DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString() != "")
                    {
                        
                        consumeMod = consume.GetModel(int.Parse(DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString()));
                        DgvConsumptionBill.Rows[i].Cells["CardType"].Value = consumeMod.solutionName;
                    }
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            ConsumptionBill consume = new ConsumptionBill(mainform);
            consume.ExportExcel("", DgvConsumptionBill);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer开启
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //timer关闭
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;
        private void SavingMoneyFormBill_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (sousuo == 0)
            {
                pageNum--;
                if (pageNum < 1)
                {
                    pageNum = 1;
                    MessageBox.Show("已是首页");
                    return;
                }
                this.label2.Text = pageNum + "/" + ss;
                DGV();
            }
            else
            {
                if (pageNum > 1)
                {
                    pageNum--;
                    wasteBook waste =new wasteBook();
                    DataTable dt = waste.Pagination(Convert.ToInt32(pageSize), pageNum, strWhere).Tables[0];
                    DgvConsumptionBill.DataSource = dt;
                    consumeSolution consume = new consumeSolution();
                    Model.consumeSolution consumeMod = new Model.consumeSolution();

                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString() != "")
                            {
                                consumeMod = consume.GetModel(int.Parse(DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString()));
                                DgvConsumptionBill.Rows[i].Cells["CardType"].Value = consumeMod.solutionName;
                            }
                        }
                    }
                    this.label2.Text = pageNum + "/" + ss;
                }
                else
                {
                    MessageBox.Show("已是首页");
                }
            }
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (sousuo == 0)
            {
                if (pageNum == ss)
                {
                    MessageBox.Show("已是最大页数");
                }
                else
                {
                    pageNum++;
                }
                this.label2.Text = pageNum + "/" + ss;
                DGV();
            }
            else
            {
                if (pageNum <ss)
                {
                    pageNum++;
                    wasteBook waste = new wasteBook();
                    DataTable dt = waste.Pagination(Convert.ToInt32(pageSize), pageNum, strWhere).Tables[0];
                    DgvConsumptionBill.DataSource = dt;
                    consumeSolution consume = new consumeSolution();
                    Model.consumeSolution consumeMod = new Model.consumeSolution();

                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString() != "")
                            {
                                consumeMod = consume.GetModel(int.Parse(DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString()));
                                DgvConsumptionBill.Rows[i].Cells["CardType"].Value = consumeMod.solutionName;
                            }
                        }
                    }
                    this.label2.Text = pageNum + "/" + ss;
                }
                else
                {
                    MessageBox.Show("已是首页");
                }
            }
  
        }

        private void DgvConsumptionBill_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (DgvConsumptionBill.Rows.Count > 0)
            {
                for (int i = 0; i < DgvConsumptionBill.Rows.Count; i++)
                {
                    if (DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString() != "")
                    {
                        consumeSolution consume = new consumeSolution();
                        Model.consumeSolution consumeMod = new Model.consumeSolution();
                        consumeMod = consume.GetModel(int.Parse(DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString()));
                        DgvConsumptionBill.Rows[i].Cells["CardType"].Value = consumeMod.solutionName;
                    }
                }
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                
                int lb2 = int.Parse(this.textBox1.Text);
                if (lb2 < 1 || lb2 > ss)
                {
                    MessageBox.Show("没有此页");
                }
                else
                {
                    pageNum = lb2;
                    wasteBook waste=new wasteBook();
                    DataTable dt = waste.Pagination(Convert.ToInt32(pageSize), pageNum, strWhere).Tables[0];
                    DgvConsumptionBill.DataSource = dt;
                    consumeSolution consume = new consumeSolution();
                    Model.consumeSolution consumeMod = new Model.consumeSolution();
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString() != "")
                            {
                                consumeMod = consume.GetModel(int.Parse(DgvConsumptionBill.Rows[i].Cells["consumeType"].Value.ToString()));
                                DgvConsumptionBill.Rows[i].Cells["CardType"].Value = consumeMod.solutionName;
                            }
                        }
                    }
                    this.label2.Text = pageNum + "/" + ss;

                }
            }
            catch (Exception)
            {
                MessageBox.Show("请输入合法内容！");
            }
       
        }

        private void TxtConsumer_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }
    }
}
